package com.red.wine.ui;


import com.red.wine.R;
import com.red.wine.WineApp;
import com.red.wine.util.IntentUtil;
import com.red.wine.util.PrefsUtils;
import com.red.wine.util.StringUtil;
import com.red.wine.view.MyScrollLayout;
import com.red.wine.view.OnViewChangeListener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class WelcomActivity extends Activity implements OnViewChangeListener, OnClickListener{
    /** Called when the activity is first created. */

	private MyScrollLayout mScrollLayout;	
	private ImageView[] mImageViews;	
	private int mViewCount;	
	private int mCurSel;
	Context context;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        context = this;
        if(!StringUtil.isEmptyOrNull(PrefsUtils.getFirst())){
        	sleepToNextPage();
        }else{
        	init();
        }
        
        Log.v("@@@@@@", "this is in  SwitchViewDemoActivity onClick()");
    }
    @Override
	protected void onResume() {
		if (WineApp.getInstance().isWiFiActive()
				|| WineApp.getInstance().isNetworkAvailable()) {
			// 发起定位，设置定位监听
			WineApp.getInstance().getmLocationClient().start();
		} else {
			Toast.makeText(context,
					getResources().getString(R.string.network_error),
					Toast.LENGTH_SHORT).show();
		}
		super.onResume();
	}
    
    private ImageView btn;
    private void init()
    {
    	mScrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout); 
    	btn = (ImageView) findViewById(R.id.welcom_to_register);
    	LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llayout);   	
    	mViewCount = mScrollLayout.getChildCount();
    	mImageViews = new ImageView[mViewCount];   	
    	for(int i = 0; i < mViewCount; i++)    	{
    		mImageViews[i] = (ImageView) linearLayout.getChildAt(i);
    		mImageViews[i].setEnabled(true);
    		mImageViews[i].setOnClickListener(this);
    		mImageViews[i].setTag(i);
    	}    	
    	mCurSel = 0;
    	mImageViews[mCurSel].setEnabled(false);    	
    	mScrollLayout.SetOnViewChangeListener(this);
    	mScrollLayout.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int pos = (Integer)(v.getTag());
//				setCurPoint(pos);
				Log.e("TAG", "POS: = " + pos);
//				mScrollLayout.snapToScreen(pos);
			}
		});
    	Log.v("@@@@@@", "this is in  SwitchViewDemoActivity init()");
    	
    	btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sleepToNextPage();
			}
		});
    }

    private void sleepToNextPage() {
//		new AsyncTask<Void, Void, Void>() {
//			@Override
//			protected Void doInBackground(Void... params) {
//				// Log.e("tag", "跳转到lognin");
//				SystemClock.sleep(2000);
				IntentUtil.redirectToNext(context,
						MainActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TOP);
				 finish();
//				return null;
//			}
//		}.execute();
	}
    
    private void setCurPoint(int index)
    {
    	if (index < 0 || index > mViewCount - 1 || mCurSel == index)    	{
    		return ;
    	}    	
    	mImageViews[mCurSel].setEnabled(true);
    	mImageViews[index].setEnabled(false);    	
    	mCurSel = index;
    }

	public void OnViewChange(int view) {
		// TODO Auto-generated method stub
		setCurPoint(view);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		int pos = (Integer)(v.getTag());
		setCurPoint(pos);
//		Log.e("TAG", "POS: = " + pos);
		mScrollLayout.snapToScreen(pos);
	}

	public void OnViewDown(View v) {
		int pos = (Integer)(v.getTag());
		setCurPoint(pos);
		Log.e("TAG", "POS: = " + pos);
	}

	public void OnViewDown(int view) {
		Log.e("TAG", "POS view: = " + view);
	}
}